package com.ebt.m.jpush;

import android.os.Bundle;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

/**
 * JPush Data
 * Created by archer.qi on 2016/6/7.
 */
public class JPushData {

    // Keys for Message

    public static final String JPUSH_DATA_TITLE = JPushInterface.EXTRA_TITLE;
    public static final String JPUSH_DATA_MESSAGE = JPushInterface.EXTRA_MESSAGE;
    public static final String JPUSH_DATA_CONTENT_TYPE = JPushInterface.EXTRA_CONTENT_TYPE;
    public static final String JPUSH_DATA_APPKEY = JPushInterface.EXTRA_APP_KEY;

    public static final String JPUSH_DATA_MSG_ID = JPushInterface.EXTRA_MSG_ID;
    public static final String JPUSH_DATA_EXTRA = JPushInterface.EXTRA_EXTRA;
    public static final String JPUSH_DATA_REGISTERID = JPushInterface.EXTRA_REGISTRATION_ID;

    // Keys for Notification

    public static final String JPUSH_DATA_NOTIFICATION_ID = JPushInterface.EXTRA_NOTIFICATION_ID;
    public static final String JPUSH_DATA_NOTIFICATION_CONTENT_TITLE = JPushInterface.EXTRA_NOTIFICATION_TITLE;
    public static final String JPUSH_DATA_ALERT = JPushInterface.EXTRA_ALERT;

    // keys for data from server

    public static final String SERVER_DATA_TOPIC = "topic";
    public static final String SERVER_DATA_AGENT_ID = "agentId";
    public static final String SERVER_DATA_ACTION = "action";
    public static final String SERVER_DATA_MESSAGE = "data";


    // 将JPush数据转化为Json
    public static String parseJson(Bundle bundle) {
        if (bundle == null) {
            return null;
        }
        JSONObject json = new JSONObject();

        for (String key : bundle.keySet()) {
            if (null != key && !"".equals(key)) {
                try {
                    json.put(key, bundle.get(key) == null ? "" : bundle.get(key));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        return json.toString();
    }

    /**
     * 从Json中解析出JPush结构的实体
     * <br>如果原有JPush解析实现不需要改变,则可以通过该方法得到JPushResult的实体<br/>
     *
     * @param json
     * @return
     */
    public static JPushResult getJPushResult(String json) {
        try {
            JPushResult result = new JPushResult();
            JSONObject obj = new JSONObject(json);
            if (obj.has(JPUSH_DATA_TITLE)) {
                result.title = obj.optString(JPUSH_DATA_TITLE);
            }
            if (obj.has(JPUSH_DATA_MESSAGE)) {
//                result.message = getMsg(obj.optString(JPUSH_DATA_MESSAGE));
                result.message = getMsgNew(obj.optString(JPUSH_DATA_MESSAGE));
            }
            if (obj.has(JPUSH_DATA_CONTENT_TYPE)) {
                result.contentType = obj.optString(JPUSH_DATA_CONTENT_TYPE);
            }
            if (obj.has(JPUSH_DATA_APPKEY)) {
                result.appKey = obj.optString(JPUSH_DATA_APPKEY);
            }
            if (obj.has(JPUSH_DATA_MSG_ID)) {
                result.msgId = obj.optString(JPUSH_DATA_MSG_ID);
            }
            if (obj.has(JPUSH_DATA_EXTRA)) {
                result.extra = obj.optString(JPUSH_DATA_EXTRA);
            }
            return result;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JPushResult getJPushResult(Bundle bundle) {
        if (bundle == null) {
            return null;
        }
        JPushResult result = new JPushResult();
        if (bundle.containsKey(JPUSH_DATA_TITLE)) {
            result.title = bundle.getString(JPUSH_DATA_TITLE);
        }
        if (bundle.containsKey(JPUSH_DATA_MESSAGE)) {//极光的消息数据封装在EXTRA_MESSAGE里
//            result.message = getMsg(bundle.getString(JPUSH_DATA_MESSAGE));
            result.message = getMsgNew(bundle.getString(JPUSH_DATA_MESSAGE));
        }
        if (bundle.containsKey(JPUSH_DATA_CONTENT_TYPE)) {
            result.contentType = bundle.getString(JPUSH_DATA_CONTENT_TYPE);
        }
        if (bundle.containsKey(JPUSH_DATA_EXTRA)) {//极光的通知数据封装在extra里
            result.extra = bundle.getString(JPUSH_DATA_EXTRA);
        }
        if (bundle.containsKey(JPUSH_DATA_APPKEY)) {
            result.appKey = bundle.getString(JPUSH_DATA_APPKEY);
        }
        if (bundle.containsKey(JPUSH_DATA_MSG_ID)) {
            result.msgId = bundle.getString(JPUSH_DATA_MSG_ID);
        }
        return result;
    }

//    public static JPushMsg getMsg(String message) {
//        if (TextUtils.isEmpty(message)) {
//            return null;
//        }
//        JPushMsg jpushMsg = new JPushMsg();
//        try {
//            JSONObject obj = new JSONObject(message);
//            jpushMsg.topic = obj.optString(SERVER_DATA_TOPIC);
//            jpushMsg.action = obj.optString(SERVER_DATA_ACTION);
//            jpushMsg.agentId = obj.optString(SERVER_DATA_AGENT_ID);
//            jpushMsg.dataJson = obj.getJSONObject(SERVER_DATA_MESSAGE);
//        } catch (JSONException ex) {
//            ex.printStackTrace();
//        }
//        return jpushMsg;
//    }

    /**
     * @param message
     * @return
     */
    public static JPushMsg getMsgNew(String message) {
        if (TextUtils.isEmpty(message)) {
            return null;
        }
        JPushMsg jpushMsg = new JPushMsg();
        try {
            jpushMsg.dataJson = new JSONObject(message);
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return jpushMsg;
    }

}
